package com.ysd.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysd.entity.User;
import com.ysd.service.UserService;
import com.ysd.util.Result;

@Controller
public class UserController {
	@Autowired
	UserService userService;

	@RequestMapping("login")
	@ResponseBody
	public String login(User user) {
		User u = userService.findUser(user);
		if (u != null) {
			if (u.getPassword().equals(user.getPassword())) {
				return Result.toClient("0", "登录成功", null, u);
			} else {
				return Result.toClient("1", "账号或密码错误", null, null);
			}
			
		} else {
			return Result.toClient("1","账号或密码错误",null); 
		}
	}

	@RequestMapping("addUser")
	@ResponseBody
	public Object addUser(User user) {
		User u=userService.findUser(user);
		
		if (u != null && u.getUsername().equals(user.getUsername())) {
			
				return Result.toClient("1", "此账号已存在", null);
			
		} else {
			int n = userService.insertUser(user);
			if (n>0) {//注册成功
				return Result.toClient("0", "注册成功", n);
			} else {
				return Result.toClient("1", "注册失败", 0);
			} 
		}
		
	}
}
